package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.domain.Setmeal;
import com.example.domain.other.SetmealDish;
import com.example.dto.SetmealDishDto;
import com.example.mapper.SetmealMapper;
import com.example.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper,Setmeal> implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishServiceImpl setmealDishService;

    //新增套餐
    @Override
    @Transactional
    public void saveWithSetmeal(SetmealDishDto setmealDto) {
        //保存套餐的基本信息，操作setmeal,执行insert操作
        //this.save(setmealDto);
        //给SetmealDish集合中的SetmealID属性添加值
        //List<SetmealDish> setmealDishs=setmealDto.getSetmealDishes();
//        setmealDishs.stream().map((item)->{
//            item.setSetmealId(setmealDto.getId());
//            return item;
//        }).collect(Collectors.toList());
//        //保存套餐和菜品的关联信息，操作setmeal_dish，执行insert操作
//        setmealDishService.saveBatch(setmealDishs);
    }
    //分页查询
    @Override
    public void getSetmealDtoPage(IPage<SetmealDishDto> iPage, String name) {
        setmealMapper.getSetmealDtoPage(iPage,name);
    }
    //删除套餐
    @Override
    @Transactional
    public void removeWithDish(List<Long> ids) {
        //查询
        LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId,ids);
        queryWrapper.eq(Setmeal::getStatus,1);
        int count = this.count(queryWrapper);
        if(count>0){
            throw new RuntimeException("套餐正在售卖中,请关闭后删除!");
        }
        //删除setmeal表中的数据
        this.removeByIds(ids);
        //删除关系表中的数据
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(lambdaQueryWrapper);
    }

}
